package com.example.administrator.taxicar.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.taxicar.R;
import com.example.administrator.taxicar.utils.Constants;
import com.example.administrator.taxicar.utils.SearchAutoAdapter;
import com.example.administrator.taxicar.utils.SearchAutoData;

import java.util.ArrayList;
import java.util.Arrays;

public class AdressActivity extends Activity implements View.OnClickListener {
    private TextView adress_text;
    private EditText shuru_adress;
    private ImageView title_left_img;
    private Button queding_button;

    private ListView mAutoListView;
    private SearchAutoAdapter mSearchAutoAdapter;
    private String adress_start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题
        setContentView(R.layout.activity_adress_layout);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.adress_activity_titlebar_layout);//自定义布局赋

        Intent intent = getIntent();
        adress_start = intent.getStringExtra(Constants.START);

        adress_text = (TextView) findViewById(R.id.adress_text);
        adress_text.setOnClickListener(this);
        queding_button = (Button) findViewById(R.id.queding_text);
        queding_button.setOnClickListener(this);
        title_left_img = (ImageView) findViewById(R.id.title_left_img);
        title_left_img.setOnClickListener(this);
        shuru_adress = (EditText) findViewById(R.id.shuru_adress);
        shuru_adress.setHint(adress_start);

        getCity();

        mSearchAutoAdapter = new SearchAutoAdapter(this, -1, this);
        mAutoListView = (ListView) findViewById(R.id.auto_listview);
        mAutoListView.setAdapter(mSearchAutoAdapter);
        mAutoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,
                                    long arg3) {
                SearchAutoData data = (SearchAutoData) mSearchAutoAdapter.getItem(position);
                shuru_adress.setText(data.getContent());
            }
        });

        shuru_adress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mSearchAutoAdapter.performFiltering(s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (shuru_adress.length() > 0) {
                    queding_button.setVisibility(View.VISIBLE);
                } else {
                    queding_button.setVisibility(View.GONE);
                }
            }
        });
    }

    public void getCity() {
        SharedPreferences sharedPreferences = getSharedPreferences(Constants.ADRESS, this.MODE_PRIVATE);
        String city = sharedPreferences.getString(Constants.ADRESS_CITY, "");
        //保存当前的城市名
        adress_text.setText(city);
        String location=sharedPreferences.getString(Constants.ADRESS_STR,"");
        //保存当前的地址
        saveSearchHistory(location);
    }

    @Override
    public void onClick(View v) {
        SharedPreferences sp = getSharedPreferences(Constants.ADRESS_SHURU, this.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        SharedPreferences.Editor editor2 = sp.edit();
        String shuru = shuru_adress.getText().toString();
        switch (v.getId()) {
            case R.id.adress_text:
                startActivityForResult(new Intent(AdressActivity.this, ActivitySelectCity.class), Constants.ADRESS_ACTIVITY);
                break;
            case R.id.queding_text:
                String text = shuru_adress.getText().toString().trim();
                saveSearchHistory(text);
                mSearchAutoAdapter.initSearchHistory();
                Log.e("tag", "adress_start:" + adress_start);
                if (adress_start.equals("从哪儿上车")) {
                    //将输入的地址保存
                    editor.putString(Constants.ADRESS_SELECTOR, shuru_adress.getText().toString());
                    editor.commit();
                } else if (adress_start.equals("要去哪儿")) {
                    //将输入的目的地保存
                    editor2.putString(Constants.ADRESS_END, shuru_adress.getText().toString());
                    editor2.commit();
                }
                finish();
                break;
            case R.id.title_left_img:
                if (shuru.equals("")) {
                    editor.clear();
                    editor.commit();
                    editor2.clear();
                    editor2.commit();
                }
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            switch (resultCode) {
                case Constants.ADRESS_ACTIVITY:
                    adress_text.setText(data.getStringExtra("lngCityName"));
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 保存搜索记录
	 */
    private void saveSearchHistory(String text) {
        if (text.length() < 1) {
            return;
        }
        SharedPreferences sp = getSharedPreferences(Constants.SEARCH_HISTORY, 0);
        String longhistory = sp.getString(Constants.SEARCH_HISTORY, "");
        String[] tmpHistory = longhistory.split(",");
        ArrayList<String> history = new ArrayList<>(
                Arrays.asList(tmpHistory));
        if (history.size() > 0) {
            int i;
            for (i = 0; i < history.size(); i++) {
                if (text.equals(history.get(i))) {
                    history.remove(i);
                    break;
                }
            }
            history.add(0, text);
        }
        if (history.size() > 0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < history.size(); i++) {
                sb.append(history.get(i) + ",");
            }
            sp.edit().putString(Constants.SEARCH_HISTORY, sb.toString()).commit();
        } else {
            sp.edit().putString(Constants.SEARCH_HISTORY, text + ",").commit();
        }
    }
}
